<template>
  <view>
    <view class="goods-item">
      <!-- 左侧图片 -->
      <view class="goods-item-left">
         <radio :checked="goods.goods_state" color="#C00000"  v-if="showRadio" @click="radioChangeHandler"></radio>
         <image :src="goods.goods_small_logo || defaultPic" mode="widthFix"></image>
      </view>
      <!-- 右侧文字显示（上面商品标题，下面商品价格） -->
      <view class="goods-item-right">
        <view class="goods-name">
          {{goods.goods_name}}
        </view>
        <view class="goods-price">
          <view class="goods-price-title">
            ￥:{{goods.goods_price | toFixed}}
          </view>
          <!-- 商品数量 -->
          <uni-number-box :min="1" :value="goods.goods_count" @change="numChangeHandler" v-if="showNum"></uni-number-box>
        </view>
      
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name:"my-goods",
    // 定义过滤器
    filters:{
      toFixed(num){
        return Number(num).toFixed(2)
      }
    },
    props:{
      goods:{
        type: Object,
        default: {}
      },
      // 定义控制自定义单选按钮显示/隐藏的属性
      showRadio:{
        type:Boolean,
        default:false
      },
      // 定义数字框的显示/隐藏
      showNum:{
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        // 默认的空图片
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
      };
    },
    methods:{
      // 点击单选按钮,控制当前购物车选中商品的状态
      radioChangeHandler(e){
        // 触发父组件的事件,同时传入值
        this.$emit("radio-change",{
             // 商品的 Id
              goods_id: this.goods.goods_id,
              // 商品最新的勾选状态
              goods_state: !this.goods.goods_state
        })
      },
      // 修改商品数量
      numChangeHandler(val){
        console.log("val:",val)
        // 触发父组件的事件
        this.$emit("num-change",{
          goods_id: this.goods.goods_id,
          goods_count: +val
        })
      },
      
    }
  }
</script>

<style lang="scss">
 .goods-item {
      display: flex;
      border-bottom: 1px solid #efefef;
      padding: 5px 0px;

      .goods-item-left {
        display: flex;
        align-items: center;
        padding-left:3px;
        image {
          width: 100px;
          width: 100px;
        }

        margin-right: 5px;
      }

      .goods-item-right {
        display: flex;
        flex-direction: column;
        justify-content: space-between;

        .goods-name {
          font-size: 14px;
        }

        .goods-price {
          display: flex;
          align-items: center;
          justify-content: space-between;
          .goods-price-title{
            color: #c00;
            font-weight: bold;
            font-size: 16px;
          }
          
        }
      }
    }
</style>